﻿using Festival_Locator.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
using System.Web.ModelBinding;

namespace Festival_Locator
{
    [ServiceContract(Namespace = "Festival_Locator")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class AjaxService : BasePage
    {

        //language support
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
        }

        // To use HTTP GET, add [WebGet] attribute. (Default ResponseFormat is WebMessageFormat.Json)
        // To create an operation that returns XML,
        //     add [WebGet(ResponseFormat=WebMessageFormat.Xml)],
        //     and include the following line in the operation body:
        //         WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
        [OperationContract]
        public void DoWork()
        {


            // Add your operation implementation here
            return;
        }



        [WebGet]
        public string GetFestival([QueryString("festivalID")] int? festivalID)
        {
            var _db = new Festival_Locator.Models.groep12_festivalsEntities();

            IQueryable<Festivals> query = _db.Festivals;
            if (festivalID.HasValue && festivalID > 0)
            {
                query = query.Where(p => p.fest_id == festivalID);
            }

            var festival = query.FirstOrDefault();

            string naam, locatie, duur, datum;
            naam = festival.fest_naam;
            locatie = festival.fest_locatie;
            duur = festival.fest_duur.ToString();
            datum = festival.fest_datum.ToShortDateString();

           /* String result = "<h1>" + naam + "</h1></div><br />" + Resources.SiteResources.Location + "" + locatie + "<br />" +
            "<span><b>Duur:</b>&nbsp;" + duur + "</span><br /><span><b>Datum:</b>&nbsp;" + datum + "</span><br /><div style='display: inline-flexbox;'>" +
            "<b>Bands:</b></div><div style='display: inline-block;'><ul>";*/

            String result = "<h1>" + naam + "</h1></div><br /><span><b>Locatie:</b>&nbsp;" + locatie + "</span><br />" +
           "<span><b>Duur:</b>&nbsp;" + duur + "</span><br /><span><b>Datum:</b>&nbsp;" + datum + "</span><br /><div style='display: inline-flexbox;'>" +
           "<b>Bands:</b></div><div style='display: inline-block;'><ul>";

            List<BandsPerFestival> bandsList = festival.BandsPerFestival.Where(bpf => bpf.fest_id == festivalID).ToList();
            foreach (BandsPerFestival bpf in bandsList)
            {
                result += "<li><a href='"+bpf.Bands.band_url+"'>" + bpf.Bands.band_naam + "</a> (" + bpf.Bands.band_soortMuziek + ")</li>";
            }

            result += "</ul></div>";
            return result;
        }
        // Add more operations here and mark them with [OperationContract]
    }
}
